package siga.visao_financeiro;

import siga.visao_busca.buscaVer;
import siga.visao_busca.buscaVer_uni;
import siga.visao_busca.buscaBanco;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao.*;


public class MemoCadContaBanco extends javax.swing.JInternalFrame {


    public MemoCadContaBanco() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfCodProfessor = new javax.swing.JTextField();
        jtfDescProfessor = new javax.swing.JTextField();
        jtfCodBanco = new javax.swing.JTextField();
        jtfDescBanco = new javax.swing.JTextField();
        jbBprof = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtfAgencia = new javax.swing.JTextField();
        jtfOp = new javax.swing.JTextField();
        jtfConta = new javax.swing.JTextField();
        jbSalvar = new javax.swing.JButton();
        jbAlterar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jbNovo = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jtfTitular = new javax.swing.JTextField();
        jbBbanco = new javax.swing.JButton();
        jbBusca = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Cadastro Contas Bancárias");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/Business_People_16x16.png"))); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jLabel1.setText("Professor");
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 70, 14);

        jLabel2.setText("Banco");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 60, 70, 14);

        jtfCodProfessor.setEditable(false);
        jtfCodProfessor.setName("jtfCodProfessor"); // NOI18N
        jPanel1.add(jtfCodProfessor);
        jtfCodProfessor.setBounds(10, 30, 70, 20);

        jtfDescProfessor.setEditable(false);
        jtfDescProfessor.setName("jtfDescProfessor"); // NOI18N
        jPanel1.add(jtfDescProfessor);
        jtfDescProfessor.setBounds(90, 30, 530, 20);

        jtfCodBanco.setEditable(false);
        jtfCodBanco.setName("jtfCodBanco"); // NOI18N
        jPanel1.add(jtfCodBanco);
        jtfCodBanco.setBounds(10, 80, 70, 20);

        jtfDescBanco.setEditable(false);
        jtfDescBanco.setName("jtfDescBanco"); // NOI18N
        jPanel1.add(jtfDescBanco);
        jtfDescBanco.setBounds(90, 80, 530, 20);

        jbBprof.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBprof.setEnabled(false);
        jbBprof.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbBprof.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jbBprof.setName("jbBprof"); // NOI18N
        jbBprof.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbBprof.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBprofActionPerformed(evt);
            }
        });
        jPanel1.add(jbBprof);
        jbBprof.setBounds(630, 30, 23, 21);

        jLabel3.setText("Operação");
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel1.add(jLabel3);
        jLabel3.setBounds(150, 160, 70, 14);

        jLabel4.setText("Conta");
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4);
        jLabel4.setBounds(230, 160, 80, 14);

        jLabel5.setText("Agência");
        jLabel5.setName("jLabel5"); // NOI18N
        jPanel1.add(jLabel5);
        jLabel5.setBounds(10, 160, 70, 14);

        jtfAgencia.setEditable(false);
        jtfAgencia.setName("jtfAgencia"); // NOI18N
        jPanel1.add(jtfAgencia);
        jtfAgencia.setBounds(10, 180, 130, 20);

        jtfOp.setEditable(false);
        jtfOp.setName("jtfOp"); // NOI18N
        jPanel1.add(jtfOp);
        jtfOp.setBounds(150, 180, 70, 20);

        jtfConta.setEditable(false);
        jtfConta.setName("jtfConta"); // NOI18N
        jPanel1.add(jtfConta);
        jtfConta.setBounds(230, 180, 390, 20);

        jbSalvar.setText("Salvar");
        jbSalvar.setEnabled(false);
        jbSalvar.setMaximumSize(new java.awt.Dimension(77, 23));
        jbSalvar.setMinimumSize(new java.awt.Dimension(77, 23));
        jbSalvar.setName("jbSalvar"); // NOI18N
        jbSalvar.setPreferredSize(new java.awt.Dimension(77, 23));
        jbSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalvarActionPerformed(evt);
            }
        });
        jPanel1.add(jbSalvar);
        jbSalvar.setBounds(470, 240, 90, 23);

        jbAlterar.setText("Alterar");
        jbAlterar.setEnabled(false);
        jbAlterar.setName("jbAlterar"); // NOI18N
        jbAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAlterarActionPerformed(evt);
            }
        });
        jPanel1.add(jbAlterar);
        jbAlterar.setBounds(280, 240, 80, 23);

        jbCancelar.setText("Cancelar");
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });
        jPanel1.add(jbCancelar);
        jbCancelar.setBounds(570, 240, 90, 23);

        jbNovo.setText("Novo");
        jbNovo.setName("jbNovo"); // NOI18N
        jbNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbNovoActionPerformed(evt);
            }
        });
        jPanel1.add(jbNovo);
        jbNovo.setBounds(190, 240, 80, 23);

        jLabel6.setText("Títular");
        jLabel6.setName("jLabel6"); // NOI18N
        jPanel1.add(jLabel6);
        jLabel6.setBounds(10, 110, 70, 14);

        jtfTitular.setEditable(false);
        jtfTitular.setName("jtfTitular"); // NOI18N
        jPanel1.add(jtfTitular);
        jtfTitular.setBounds(10, 130, 610, 20);

        jbBbanco.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBbanco.setEnabled(false);
        jbBbanco.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbBbanco.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jbBbanco.setName("jbBbanco"); // NOI18N
        jbBbanco.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbBbanco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBbancoActionPerformed(evt);
            }
        });
        jPanel1.add(jbBbanco);
        jbBbanco.setBounds(630, 80, 23, 21);

        jbBusca.setText("Buscar");
        jbBusca.setName("jbBusca"); // NOI18N
        jbBusca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscaActionPerformed(evt);
            }
        });
        jPanel1.add(jbBusca);
        jbBusca.setBounds(370, 240, 90, 23);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbNovoActionPerformed
        campos(true);
        LimpaCampos();
        jbNovo.setEnabled(false);
        jbAlterar.setEnabled(false);
        jbBusca.setEnabled(false);
        jbCancelar.setEnabled(true);
        jbSalvar.setEnabled(true);
    }//GEN-LAST:event_jbNovoActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
        campos(false);
        LimpaCampos();
        jbNovo.setEnabled(true);
        jbAlterar.setEnabled(false);
        jbBusca.setEnabled(true);
        jbCancelar.setEnabled(true);
        jbSalvar.setEnabled(false);
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAlterarActionPerformed
        jbNovo.setEnabled(false);
        jbAlterar.setEnabled(false);
        jbBusca.setEnabled(false);
        jbCancelar.setEnabled(true);
        jbSalvar.setEnabled(true);
    }//GEN-LAST:event_jbAlterarActionPerformed

    private void jbBuscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscaActionPerformed
        buscaVer bv = new buscaVer(null,true);
        bv.setLocation((index.tela.width-bv.getWidth())/2, (index.tela.height-bv.getHeight())/2);
        bv.setVisible(true);
        if(buscaVer.isConfirmaBusca()){
            if(!index.LRpessoa.get(0).equals("")){
                try {
                    conexao.Query("select p.*, fb.descricao as desc_banco, p1.descricao as desc_prof  from professor p " +
                            " left join fin_contas_cheque_banco fb on fb.codigo=p.cod_banco and fb.cod_emp=p.cod_emp " +
                            " inner join pessoas p1 on p1.codigo=p.codigo and p1.cod_emp=p.cod_emp " +
                            " where p.codigo=" + index.LRpessoa.get(0) + " and p.cod_emp=" + index.cod_emp);
                    conexao.rs.next();
                    jtfCodProfessor.setText((conexao.rs.getString("codigo")!=null)?conexao.rs.getString("codigo"):"");
                    jtfDescProfessor.setText((conexao.rs.getString("desc_prof")!=null)?conexao.rs.getString("desc_prof"):"");
                    jtfTitular.setText((conexao.rs.getString("descricao")!=null)?conexao.rs.getString("descricao"):"");
                    jtfCodBanco.setText((conexao.rs.getString("cod_banco")!=null)?conexao.rs.getString("cod_banco"):"");
                    jtfDescBanco.setText((conexao.rs.getString("desc_banco")!=null)?conexao.rs.getString("desc_banco"):"");
                    jtfAgencia.setText((conexao.rs.getString("agencia")!=null)?conexao.rs.getString("agencia"):"");
                    jtfOp.setText((conexao.rs.getString("op")!=null)?conexao.rs.getString("op"):"");
                    jtfConta.setText((conexao.rs.getString("conta")!=null)?conexao.rs.getString("conta"):"");
                    campos(true);
                    jbNovo.setEnabled(false);
                    jbAlterar.setEnabled(true);
                    jbBusca.setEnabled(true);
                    jbCancelar.setEnabled(true);
                    jbSalvar.setEnabled(false);
                } catch (SQLException ex) {
                    verErros.setErro(ex.getMessage());
                }
            }
        }
    }//GEN-LAST:event_jbBuscaActionPerformed

    private void jbBprofActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBprofActionPerformed
        buscaVer_uni bv_uni = new buscaVer_uni(null,true);
        bv_uni.setLocation((index.tela.width-bv_uni.getWidth())/2, (index.tela.height-bv_uni.getHeight())/2);
        bv_uni.setVisible(true);
        if(buscaVer_uni.isConfirmaBusca()){
            jtfCodProfessor.setText(index.LRpessoa.get(0).toString());
            jtfDescProfessor.setText(index.LRpessoa.get(1).toString());
            index.LRpessoa.set(0,"");
            index.LRpessoa.set(1,"");
        }
    }//GEN-LAST:event_jbBprofActionPerformed

    private void jbBbancoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBbancoActionPerformed
        buscaBanco bb = new buscaBanco(null,true);
        bb.setLocation((index.tela.width-bb.getWidth())/2, (index.tela.height-bb.getHeight())/2);
        bb.setVisible(true);
        if(buscaBanco.isConfirmaBanco()){
            jtfCodBanco.setText(index.LRbanco.get(0).toString());
            jtfDescBanco.setText(index.LRbanco.get(1).toString());
            index.LRbanco.set(0,"");
            index.LRbanco.set(1,"");
        }
    }//GEN-LAST:event_jbBbancoActionPerformed

    private void jbSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarActionPerformed
        try {
            if(!jtfCodProfessor.getText().equals("")){
                if(!jtfCodBanco.getText().equals("")){
                    if(!jtfAgencia.getText().equals("")){
                        if(!jtfConta.getText().equals("")){
                           conexao.Inserir("insert into pessoas(cod_emp,tipo,descricao) values (" + index.cod_emp + ",\"Pessoa\",\"" + jtfDescProfessor.getText() + "\")");
                           conexao.Query("select codigo from pessoas where cod_emp=" + index.cod_emp + " and descricao=\"" + jtfDescProfessor.getText() + "\"");
                           conexao.rs.next();
                           String codigo = conexao.rs.getString("codigo");
                           String operacao = (jtfOp.getText().equals(""))?"null":"\""+jtfOp.getText()+"\"";
                           String titular = (jtfTitular.getText().equals(""))?"\""+jtfDescProfessor.getText()+"\"":"\""+jtfTitular.getText()+"\"";
                           conexao.Inserir("insert into professor (codigo, cod_emp,descricao,cod_banco,cod_uni,agencia,op,conta) " +
                                    " values ("+codigo+","+index.cod_emp+","+titular+","+jtfCodBanco.getText()+","+jtfCodProfessor.getText()+",\""+jtfAgencia.getText()+"\","+operacao+",\""+jtfConta.getText()+"\")");

                           LimpaCampos();
                           campos(false);
                           jbNovo.setEnabled(true);
                           jbAlterar.setEnabled(false);
                           jbBusca.setEnabled(true);
                           jbCancelar.setEnabled(true);
                           jbSalvar.setEnabled(false);
                        }else{
                           JOptionPane.showMessageDialog(null, "Obrigatório preenchimento o Campo Conta.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
                           jtfConta.requestFocus();
                        }
                    }else{
                        JOptionPane.showMessageDialog(null, "Obrigatório preenchimento o Campo Agência.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
                        jtfAgencia.requestFocus();
                    }
                }else{
                    JOptionPane.showMessageDialog(null, "Obrigatório preenchimento o Campo Banco.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
                    jtfCodBanco.requestFocus();
                }
            }else{
                JOptionPane.showMessageDialog(null, "Obrigatório preenchimento o Campo Professor.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
                jtfCodProfessor.requestFocus();
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }//GEN-LAST:event_jbSalvarActionPerformed
    private void campos(boolean tipo){
        jtfAgencia.setEditable(tipo);
        jtfOp.setEditable(tipo);
        jtfConta.setEditable(tipo);
        jtfTitular.setEditable(tipo);
        jbBprof.setEnabled(tipo);
        jbBbanco.setEnabled(tipo);
    }

    private void PegaDados(){
        try {
            conexao.Query("select * from pessoas where codigo=" + jtfCodProfessor.getText());
            conexao.rs.next();
            jtfConta.setText(conexao.rs.getString("conta"));
            jtfAgencia.setText(conexao.rs.getString("agencia"));
            jtfOp.setText(conexao.rs.getString("operacao"));
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    private void LimpaCampos(){
        jtfCodProfessor.setText("");
        jtfDescProfessor.setText("");
        jtfTitular.setText("");
        jtfCodBanco.setText("");
        jtfDescBanco.setText("");
        jtfAgencia.setText("");
        jtfOp.setText("");
        jtfConta.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbAlterar;
    private javax.swing.JButton jbBbanco;
    private javax.swing.JButton jbBprof;
    private javax.swing.JButton jbBusca;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbNovo;
    private javax.swing.JButton jbSalvar;
    private javax.swing.JTextField jtfAgencia;
    private javax.swing.JTextField jtfCodBanco;
    private javax.swing.JTextField jtfCodProfessor;
    private javax.swing.JTextField jtfConta;
    private javax.swing.JTextField jtfDescBanco;
    private javax.swing.JTextField jtfDescProfessor;
    private javax.swing.JTextField jtfOp;
    private javax.swing.JTextField jtfTitular;
    // End of variables declaration//GEN-END:variables

}
